import Vue from 'vue'
import Vuex from 'vuex'
import router from '../router/index'
import persistedState from 'vuex-persistedstate'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    globalLoading: false,
    token: '',
    is_login: false,
    uin: '',
    nick: ''
  },
  mutations: {
    openGlobalLoading(state) {
      state.globalLoading = true
    },
    closeGlobalLoading(state) {
      state.globalLoading = false
    },
    setToken(state, token) {
      state.token = token
    },
    setLogin(state, login) {
      state.is_login = login
    },
    setNick(state, nick) {
      state.nick = nick
    },
    setUin(state, uin) {
      state.uin = uin
    }
  },
  actions: {
    login({ commit }, { token, uin, nick }) {

      commit('setLogin', true)
      commit('setToken', token)
      commit('setNick', nick)
      commit('setUin', uin)
      Vue.prototype.$notification.success({
        message: '登录成功',
        description: `欢迎您，${nick}`,
        placement: 'bottomRight',
      })
      router.push({ name: 'Home' })
    },
    exit({ commit }) {
      commit('setLogin', false)
      commit('setToken', '')
      router.push({ name: 'Home' })
    }
  },
  modules: {

  },
  plugins: [
    persistedState({ storage: window.sessionStorage })
  ]
})
